import sys
import subprocess
import tkinter.messagebox
import os
import json

def json_read(json_path:str)->json:
    with open(json_path,'r',encoding='utf8')as fp:
        json_data = json.load(fp)
    return json_data

def json_write(path:str,key:str,value:str):
    key = str(key)
    path = str(path)
    def get_json_data():
        with open(path, 'rb') as f:
            params = json.load(f)
            params[key] = value
            print("写入配置文件信息：", params)
        return params
    def write_json_data(params):
        with open(path, 'w') as r:
            json.dump(params, r)
    the_revised_dict = get_json_data()
    write_json_data(the_revised_dict)

if __name__ == "__main__":
    WorkSpace = sys.path[0]
    ncm1_path = WorkSpace+"\\ncm1\\mcntomusic.py"
    ncm2_path = WorkSpace+"\\ncm2\\mcntomusic.py"
    ncm3_path = WorkSpace+"\\ncm3\\mcntomusic.py"
    ncm4_path = WorkSpace+"\\ncm4\\mcntomusic.py"
    ncm5_path = WorkSpace+"\\ncm5\\mcntomusic.py"
    #print(WorkSpace+".\\Global_List.json")
    Files_json = json_read(WorkSpace+"\\Global_List.json")
    Files_list = Files_json["list_global"]
    count = len(Files_list)

    list_length = count
    list_per = int(list_length / 5)
    total_per = list_per * 5
    if total_per < list_length:
        ncm1=list_per
        ncm2=list_per
        ncm3=list_per
        ncm4=list_per
        ncm5=list_per+(list_length-total_per)
    elif total_per == list_length:
        ncm1=list_per
        ncm2=list_per
        ncm3=list_per
        ncm4=list_per
        ncm5=list_per
    else:
        pass
    ncm1_list = Files_list[:ncm1]
    del Files_list[:ncm1]
    ncm2_list = Files_list[:ncm2]
    del Files_list[:ncm2]
    ncm3_list = Files_list[:ncm3]
    del Files_list[:ncm3]
    ncm4_list = Files_list[:ncm4]
    del Files_list[:ncm4]
    ncm5_list = Files_list[:ncm5]
    del Files_list[:ncm5]
    json_write(WorkSpace+"\\ncm1\\processing_list.json", "abc", ncm1_list)
    json_write(WorkSpace+"\\ncm2\\processing_list.json", "abc", ncm2_list)
    json_write(WorkSpace+"\\ncm3\\processing_list.json", "abc", ncm3_list)
    json_write(WorkSpace+"\\ncm4\\processing_list.json", "abc", ncm4_list)
    json_write(WorkSpace+"\\ncm5\\processing_list.json", "abc", ncm5_list)
    subprocess.Popen(['python.exe', ncm1_path])
    subprocess.Popen(['python.exe', ncm2_path])
    subprocess.Popen(['python.exe', ncm3_path])
    subprocess.Popen(['python.exe', ncm4_path])
    subprocess.Popen(['python.exe', ncm5_path])